package com.lzc.tlmail.tlmailorder.controller;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzc.common.exception.RRException;
import com.lzc.common.vo.MemberEntityVo;
import com.lzc.tlmail.tlmailorder.config.AlipayTemplate;
import com.lzc.tlmail.tlmailorder.entity.OrderEntity;
import com.lzc.tlmail.tlmailorder.interceptor.OrderInterceptor;
import com.lzc.tlmail.tlmailorder.service.OrderService;
import com.lzc.tlmail.tlmailorder.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author lzc
 * @create 2020-10-20 13:27
 */
@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AlipayTemplate alipayTemplate;


    /**
     * http://www.order.tlmail.com/orderList?charset=utf-8
     * &out_trade_no=202010241129041021319843321903964162
     * &method=alipay.trade.page.pay.return
     * &total_amount=3899.00&sign=oqvhwLLWQ9j8rDJwBRnyAzsnsj%2FvB78BSrwuviXKL8exVEiAo%2BJsPYfQ4JsMpz0BDfqPTi21icO957H5fy4PtHBaHoWpaCsOsUHDV9c8W4UioNf7icB4gQF5spKlQdzD4vrLs%2Be5fOajLm917o63qingH3gohVyvBKZCpUEXtXUge1MdTj%2BZvCxsMLRWw%2F2mK11t3zA0EIkE7FA96OUcVIQtyClwHV48w%2By3RBiP0SuSeCjtc51qnSuNZGPXD4K%2FppD%2B2ezr894Y6V75YZQWPc5Jn4AfT3hnWZvhX%2BB4zskY76LF7K6i8QW0t5Y8LZ9ielY0N1oE9BKp%2BzhG4Evj1A%3D%3D&trade_no=2020102422001416280505692493&auth_app_id=2021000116681926&version=1.0&app_id=2021000116681926&sign_type=RSA2&seller_id=2088621954989362&timestamp=2020-10-24+11%3A30%3A54
     * 跳转到订单列表页
     * @return
     */
    @GetMapping("/orderList")
    public String orderList(Model model,String out_trade_no){
       List<Ordervo> ordervos=orderService.orderLists(out_trade_no);
        model.addAttribute("ordervos",ordervos);
        return "list";
    }


    @GetMapping("/{page}.html")
    public String toOrder(@PathVariable("page")String page){
        return page;
    }

    /**
     * 去结算页得到所有相关的数据展示
     * @return
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
       OrderItemvo orderItemvo=orderService.getOrder();
       model.addAttribute("orderItemvo",orderItemvo);
        return "jiesuan";
    }

    /**
     * 订单提交
     * @return
     */
    @PostMapping("/orderSubmit")
    public String orderSubmit(SubmitOrdervo submitOrdervo,Model model, RedirectAttributes redirectAttributes){
        try {
            OrderSubmitrspnvo submit=orderService.orderSubmit(submitOrdervo);
            if(submit.getResultCode()==0){
                model.addAttribute("orderrspn",submit);
                return "shouyin";
            }else {
                String msg="下单失败：";
                switch (submit.getResultCode()){
                    case 1: redirectAttributes.addAttribute("orderFailMsg",msg=msg+"订单提交超时");break;
                    case 2: redirectAttributes.addAttribute("orderFailMsg",msg=msg+"验价失败，请核对您的商品价格是否相符");break;
                    case 3: redirectAttributes.addAttribute("orderFailMsg",msg=msg+"库存锁定失败");break;
                }
                return "redirect:http://www.order.tlmail.com/toTrade";
            }
        }catch (RRException e){
            redirectAttributes.addAttribute("orderFailMsg",e.getMsg());
            return "redirect:http://www.order.tlmail.com/toTrade";
        }
    }

    /**
     * 订单支付
     * @param ordersn  produces = "text/html 这个设置返回的格式
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String payOrder(String ordersn) throws AlipayApiException {
        OrderEntity orderEntity = orderService.getOne(new LambdaQueryWrapper<OrderEntity>().eq(OrderEntity::getOrderSn, ordersn));
        PayVo payVo = new PayVo();

        payVo.setOut_trade_no(ordersn);
        payVo.setSubject(orderEntity.getOrderSn());
        BigDecimal bigDecimal = orderEntity.getPayAmount().setScale(2, BigDecimal.ROUND_UP);
        payVo.setTotal_amount(bigDecimal.toString());
        payVo.setBody(orderEntity.getNote());
        String pay = alipayTemplate.pay(payVo);
        return pay;

    }

}
